﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Training.Library.DomainObject.Administration.Catalog;
using Training.Library.DataAccess.Administration.Catalog;

using System.Configuration;
using Assesment.Web.Support;

using Assesment.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;


namespace Training.Web.Forms.Administration.Catalog
{

    public partial class KaryawanForm :  Assesment.Web.BasePage
    {
        private string id = string.Empty;
        CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
        DepartmentRepo DepartmentDal = new DepartmentRepo(ConfigurationManager.ConnectionStrings["JobdescConn"].ConnectionString);
        LokasiRepo LokasiDal = new LokasiRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
        KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
        protected override void OnLoad(EventArgs e)
        {
            try
            {

                if (Request.Params["id"] != null)
                {
                    id = Convert.ToString(Request.Params["id"]);
                }

               
                List<Department> list = DepartmentDal.GetList();
                var query = list.OrderBy(x => x.DepartmentDesc);
                list = query.ToList();
                ddlDepartment.Items.Clear();

                list.ForEach(delegate(Department obj)
                {
                    ddlDepartment.Items.Add(new ListItem(obj.DepartmentDesc, obj.DeptID.ToString()));
                });

                ddlDepartment.Items.Insert(0, new ListItem("-- pilih departemen --", string.Empty));

              
                List<Cabang> listCabang = CabangDal.GetList();
                var queryCabang = listCabang.OrderBy(x => x.NamaCabang);
                listCabang = queryCabang.ToList();
                ddlCabang.Items.Clear();

                listCabang.ForEach(delegate(Cabang objCabang)
                {
                    ddlCabang.Items.Add(new ListItem(objCabang.NamaCabang, objCabang.CabangID.ToString()));
                });

                ddlCabang.Items.Insert(0, new ListItem("-- pilih cabang --", string.Empty));

                List<Lokasi> listLokasi = LokasiDal.GetList();
                var queryLokasi = listLokasi.OrderBy(x => x.NamaLokasi);
                listLokasi = queryLokasi.ToList();
                ddlLokasi.Items.Clear();

                listLokasi.ForEach(delegate(Lokasi objLokasi)
                {
                    ddlLokasi.Items.Add(new ListItem(objLokasi.NamaLokasi, objLokasi.LokasiID.ToString()));
                });

                ddlLokasi.Items.Insert(0, new ListItem("-- pilih Lokasi --", string.Empty));
               

                if (!Page.IsPostBack)
                {
                    if (id.Trim().Length> 0) // Edit an existing item
                    {


                        txtNIK.ReadOnly = true;
                        PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobdescConn"].ConnectionString);
                        List<Position> listJabatan = PositionDal.GetList();
                        var queryJabatan = listJabatan.OrderBy(x => x.PositionName);
                        listJabatan = queryJabatan.ToList();
                        ddlJabatan.Items.Clear();

                        listJabatan.ForEach(delegate(Position objJabatan)
                        {
                            ddlJabatan.Items.Add(new ListItem(objJabatan.PositionName, objJabatan.PositionID.ToString()));
                        });

                        ddlJabatan.Items.Insert(0, new ListItem("-- pilih jabatan --", string.Empty));

                        
                        Karyawan obj = KaryawanDal.GetById(id.ToString());
                        if (obj != null)
                        {
                            hfID.Value = id.ToString();
                            txtNama.Text = obj.NamaKaryawan;
                            txtNIK.Text = obj.NIK;
                            Position objJabatan = PositionDal.GetById(Convert.ToInt32(obj.PositionID));
                            ddlDepartment.Items.FindByValue(objJabatan.DepartmentID.ToString()).Selected = true;
                            ddlCabang.Items.FindByValue(obj.CabangID.ToString()).Selected = true;
                            ddlLokasi.Items.FindByValue(obj.LokasiID.ToString()).Selected = true;
                            ddlJabatan.Items.FindByValue(obj.PositionID.ToString()).Selected = true;

                        }
                    }

                    GetPrivilege();


                }
            }
            catch (Exception ex) {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" +" Error :" +ex.Message + "');", true);
                return;
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    
    }

}
